//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.controller;

import ciicos.base.ActionResult;
import ciicos.base.Page;
import ciicos.base.model.module.ModuleModel;
import ciicos.base.service.ModuleService;
import ciicos.base.vo.ListVO;
import ciicos.model.AppMenuListVO;
import ciicos.model.login.UserMenuModel;
import ciicos.permission.model.authorize.AuthorizeVO;
import ciicos.permission.service.AuthorizeService;
import ciicos.util.JsonUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import ciicos.util.treeutil.ListToTreeUtil;
import ciicos.util.treeutil.SumTree;
import ciicos.util.treeutil.newtreeutil.TreeDotUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(
        tags = {"app应用"},
        value = "Menu"
)
@RestController
@RequestMapping({"/api/app/Menu"})
public class AppMenuController {
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private AuthorizeService authorizeService;
    @Autowired
    private UserProvider userProvider;

    @ApiOperation("获取菜单列表")
    @GetMapping
    public ActionResult<ListVO<AppMenuListVO>> list(Page page) {
        AuthorizeVO authorizeModel = this.authorizeService.getAuthorize(true);
        List<ModuleModel> buttonListAll = (List)authorizeModel.getModuleList().stream().filter((t) -> "App".equals(t.getCategory())).collect(Collectors.toList());
        buttonListAll = (List)buttonListAll.stream().filter((t) -> this.userProvider.get().getSystemId() != null && this.userProvider.get().getSystemId().equals(t.getSystemId())).collect(Collectors.toList());
        List<ModuleModel> buttonList = buttonListAll;
        if (StringUtil.isNotEmpty(page.getKeyword())) {
            buttonList = (List)buttonListAll.stream().filter((t) -> t.getFullName().contains(page.getKeyword())).collect(Collectors.toList());
        }

        List<UserMenuModel> list = JsonUtil.getJsonToList(ListToTreeUtil.treeWhere(buttonList, buttonListAll), UserMenuModel.class);
        List<SumTree<UserMenuModel>> menuAll = TreeDotUtils.convertListToTreeDot(list, "-1");
        List<AppMenuListVO> data = JsonUtil.getJsonToList(menuAll, AppMenuListVO.class);
        ListVO listVO = new ListVO();
        listVO.setList(data);
        return ActionResult.success(listVO);
    }
}
